package Negocios;

import CDB.Categorias;
import CDB.Detallesubcategoria;
import CDB.PersonFactory;
import CDB.Subcategoria;
import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author ISC Erick
 */
public class ControlSubcategoryDetail {

    private Session session;
    private Transaction transaction;
    private SessionFactory sessionFactory;

    public List selectAll() {
        List listSubCategory;
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            try {
                listSubCategory = session.createQuery("from Subcategoria s").list();
                return listSubCategory;
            } catch (HibernateException e) {
                JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
                return null;
            }
        } catch (HibernateException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
            return null;
        } finally {
            session.flush();
            session.close();
        }
    }

    public List selectByIdCategoria(int idCategoria) {
        List listSubCategory = null;
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            try {
                listSubCategory = session.createQuery("from Detallesubcategoria ds where ds.categorias.id = " + idCategoria).list();
                return listSubCategory;
            } catch (HibernateException e) {
                JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
                return null;
            }
        } catch (HibernateException | HeadlessException e) {
            throw e;
        } finally {
            session.flush();
            session.close();
        }
    }

    public Detallesubcategoria selectOne(int id) {
        Detallesubcategoria stock;
        try {
            Session sessions = createSession();
            stock = (Detallesubcategoria) sessions.load(Detallesubcategoria.class,
                    new Integer(id));
            sessions.close();
            return stock;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
            return null;
        }
    }

    public void add(Categorias categoria, Subcategoria subcaegoria) {
        try {
            iniciaSF();
            Detallesubcategoria detallesub = new Detallesubcategoria();
            //Creando un objeto
            detallesub.setCategorias(categoria);
            detallesub.setSubcategoria(subcaegoria);
            session.save(detallesub);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void Update(Categorias categoria, Subcategoria subcaegoria) {
        try {
            iniciaSF();
            Detallesubcategoria detallesub = new Detallesubcategoria();
            //Creando un objeto
            detallesub.setCategorias(categoria);
            detallesub.setSubcategoria(subcaegoria);
            session.save(detallesub);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void delete(int idDetalle) {
        try {
            iniciaSF();
            Detallesubcategoria detallesub = (Detallesubcategoria) session.load(Detallesubcategoria.class,
                    new Integer(idDetalle));
            session.delete(detallesub);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void deleteByIdCategoria(int idCategoria) {
        try {
            Session session = createSession();
            Transaction tx = session.beginTransaction();
            List lstDetalles = selectByIdCategoria(idCategoria);
            for (int i = 0; i < lstDetalles.size(); i++) {
                Detallesubcategoria detalleAEliminar = (Detallesubcategoria) lstDetalles.get(i);
//                Detallesubcategoria detallesub = (Detallesubcategoria) session.load(Detallesubcategoria.class,
//                        new Integer(detalleAEliminar.getIddetallesubcategoria()));
                session.delete(detalleAEliminar);
            }
            //Si no ocurrio error hago comit y termino la session
            tx.commit();
            session.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        }

    }

    public void deleteByIdSubcategoria(int idSubcategoria) {
        try {
            iniciaSF();
            Detallesubcategoria detallesub = (Detallesubcategoria) session.load(Subcategoria.class,
                    new Integer(idSubcategoria));
            session.delete(detallesub);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void iniciaSF() {
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de detalle de subcategoria.", "Error al iniciar Session",
                    0);
        }

    }

    public void cierraSF() {
        try {
            transaction.commit();
            session.flush();
            session.close();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de detalle de subcategoria.", "Error al cerrar Session",
                    0);
        }
    }

    public Session createSession() {

        Session sessions = null;
        try {
            //creando la nueva session para usar hibernate
            sessionFactory = PersonFactory.getSessionFactory();
            sessions = sessionFactory.openSession();

        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de detalle de subcategoria.", "Error al crear Session",
                    0);
            sessions.close();
        }
        return sessions;
    }
}
